package com.frank.leetcode.question_1_5;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode-cn.com/problems/two-sum/description/
 * 1. 两数之和
 * 难度: 简单
 * <p>
 * Question :
 * 给定一个整数数组和一个目标值，找出数组中和为目标值的两个数。
 * 你可以假设每个输入只对应一种答案，且同样的元素不能被重复利用。
 * 示例:
 * 给定 nums = [2, 7, 11, 15], target = 9
 * 因为 nums[0] + nums[1] = 2 + 7 = 9
 * 所以返回 [0, 1]
 * <p>
 * <p>
 * Created by zhy on 2018/5/31.
 */
public class TwoSum {
    private static Logger LOG = LoggerFactory.getLogger(TwoSum.class);

    @Test
    public void run() {
        int[] nums = new int[]{2, 7, 11, 15};
        int target = 9;

        int[] answer = twoSum(nums, target);
        System.out.println(Arrays.toString(answer));
    }

    /**
     * 初稿
     */
    private int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        if (nums == null || nums.length < 2)
            return nums;

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target )
                {
                    res[0] = i;
                    res[1] = j;
                    return res;
                }
            }
        }
        return null;
    }

    /**
     * Hash法
     */
    private int[] twoSumBak1(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= target) {
                map.put(nums[i], i);
            }
        }

        for (int i = 0; i < nums.length; i++) {
            int secNum = target - nums[i];
            Integer index;
            if ((index = map.get(secNum)) != null && i != index) {
                return new int[]{i, index};
            }
        }
        return null;
    }

    /**
     * 最佳实践
     */
    private int[] twoSumBest(int[] nums, int target) {
        int[] res = new int[2];
        if (nums == null || nums.length < 2)
            return res;


        //获得最大最小值
        int max = nums[0];
        int min = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if (max < nums[i])
                max = nums[i];
            if (min > nums[i])
                min = nums[i];
        }
        LOG.info("max:{}, min:{}, index.length:{}", max, min, max - min + 1);

        //创建区间数组
        int[] index = new int[max - min + 1];
        int other;
        for (int i = 0; i < nums.length; i++) {
            other = target - nums[i];
            //第二个数在区间内, 则记录
            if (other < min || other > max) {
                continue;
            }

            LOG.info("other - min:{}", target - nums[i] - min);
            //判断是否存在值
            //other = target - nums[i], 所以用index 的下标代表(当前值 - min)，并记录坐标,当发现另一个初始化后的数组值，则为另一个数。
            if (index[other - min] > 0) {
                res[0] = index[other - min] - 1;
                res[1] = i;
                return res;
            }
            //记录当前索引
            index[nums[i] - min] = i + 1;//加1是为了区分初始化
        }

        return res;
    }
}
